package view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

/**
 * Created by Administrator on 2017/3/28.
 * 确认对话框
 */

public class MyConfirmDialog extends Dialog {
    private Button yes;//确定按钮
    private Button no;//取消按钮
    private CancelListener cancelListener;
    private ConfirmListener confirmListener;
    private RelativeLayout mycomfirm_dialog_parent;
    private TextView mycomfirm_dialog_tv;
    private String text;
    private String confirmText;
    private String cancelText;

    public MyConfirmDialog(Context context, String text) {
        super(context, R.style.confirmDialog);
        this.text = text;
    }

    public MyConfirmDialog(Context context, int themeResId, String text) {
        super(context, themeResId);
        this.text = text;
    }

    public MyConfirmDialog(Context context, String text, String confirmText, String cancelText) {
        super(context, R.style.confirmDialog);
        this.text = text;
        this.confirmText = confirmText;
        this.cancelText = cancelText;
    }

    protected MyConfirmDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycomfirm_dialog);
        //按空白处不能取消
        setCanceledOnTouchOutside(false);
        no = (Button) findViewById(R.id.mycomfirm_dialog_cancel);
        yes = (Button) findViewById(R.id.mycomfirm_dialog_confirm);
        mycomfirm_dialog_parent = (RelativeLayout) findViewById(R.id.mycomfirm_dialog_parent);
        mycomfirm_dialog_tv = (TextView) findViewById(R.id.mycomfirm_dialog_tv);
        if(text!=null) {
            if(!TextUtils.isEmpty(text)) {
                mycomfirm_dialog_tv.setText(text);
            }
        }
        if(cancelText!=null) {
            if(!TextUtils.isEmpty(cancelText)) {
                no.setText(cancelText);
            }
        }
        if(confirmText!=null) {
            if(!TextUtils.isEmpty(confirmText)) {
                yes.setText(confirmText);
            }
        }
        mycomfirm_dialog_parent.setOnClickListener(v -> MyConfirmDialog.this.dismiss());
        no.setOnClickListener(v -> {
            MyConfirmDialog.this.dismiss();
            if (cancelListener != null) {
                cancelListener.cancel();
            }
        });
        TextPaint tp = yes.getPaint();
        tp.setFakeBoldText(true);
        yes.setOnClickListener(v -> {
            if (confirmListener != null) {
                MyConfirmDialog.this.dismiss();
                confirmListener.confirm();
            }
        });
    }

    public void setCancelListener(CancelListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public void setConfirmListener(ConfirmListener confirmListener) {
        this.confirmListener = confirmListener;
    }

    public interface CancelListener {
        void cancel();
    }

    public interface ConfirmListener {
        void confirm();
    }
}
